#include "konus_controller.h"

KonusController::KonusController()
{
	konusClient = new SimpleClient();
	konusClient->Connect("127.0.0.1:7788");
	keyDown = false;
}

KonusController::~KonusController()
{
	delete konusClient;
}

bool KonusController::GetEvent(SDL_Event &event)
{
	if (keyDown)
	{
		event.type = SDL_KEYUP;
		event.key.keysym.sym = lastEvent.key.keysym.sym;
		event.key.keysym.scancode = lastEvent.key.keysym.scancode;
		keyDown = false;
		return true;
	}
	if (konusClient->isRead())
	{
		int len;
		char* mes;
		if ((mes = konusClient->Recv(len, 2)) != NULL)
		{
			if (mes[0] == KEY_DOWN)
				event.type = SDL_KEYDOWN;
			else if (mes[0] == KEY_UP)
				event.type = SDL_KEYUP;
			else if (mes[0] == KEY_DOWN_UP) {
				event.type = SDL_KEYDOWN;
				keyDown = true;
			}
			event.key.keysym.sym = SDLKey(275);
			event.key.keysym.scancode = mes[1];
			lastEvent = event;
			delete mes;
			return true;
		}
	}
	return false;
}

void KonusController::SendMessage(char* message)
{
	konusClient->Send(message, strlen(message));
}
